package com.example.demo2.controller;

import com.example.demo2.bean.NewsBean;
import com.example.demo2.service.NewsBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*",
        methods = {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST})
public class NewsBeanController {
    @Autowired
    NewsBeanService yourBeanService;

    // 查询一个
    @RequestMapping("/app6/news/findone")
    public Map<String, Object> findone(@RequestBody NewsBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        NewsBean yourBean1 = yourBeanService.findone(yourBean);
        if (yourBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", yourBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    // 查询所有
    @RequestMapping("/app5/news/list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<NewsBean> list = yourBeanService.list();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list", list);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

//    // 删除
//    @RequestMapping("/YourServer/yourbean/delete")
//    public Map<String, Object> delete(@RequestBody NewsBean yourBean) {
//        Map<String, Object> map = new HashMap<>();
//        try {
//            yourBeanService.deleteById(yourBean.getId());
//            map.put("success", true);
//            map.put("msg", "成功");
//        } catch (Exception e) {
//            map.put("success", false);
//            map.put("msg", "失败");
//        }
//
//        return map;
//    }
//    // 修改
//    @RequestMapping("/YourServer/yourbean/update")
//    public Map<String, Object> update(@RequestBody NewsBean yourBean) {
//        Map<String, Object> map = new HashMap<>();
//        NewsBean yourBean1 = yourBeanService.update(yourBean);
//        if (yourBean1 != null) {
//            map.put("success", true);
//            map.put("msg", "成功");
//            map.put("data", yourBean1);
//        } else {
//            map.put("success", false);
//            map.put("msg", "失败");
//        }
//        return map;
//    }
//    // 添加
//    @RequestMapping("/YourServer/yourbean/addone")
//    public Map<String, Object> addone(@RequestBody NewsBean yourBean) {
//        Map<String, Object> map = new HashMap<>();
//        NewsBean yourBean1 = yourBeanService.addone(yourBean);
//        if (yourBean1 != null) {
//            map.put("success", true);
//            map.put("msg", "成功");
//            map.put("data", yourBean1);
//        } else {
//            map.put("success", false);
//            map.put("msg", "失败");
//        }
//        return map;
//    }
}

